package com.ae.shield.generator.builder.recipes.assembly;

import com.google.gson.JsonObject;
import com.ae.shield.common.items.ItemList;
import com.ae.shield.common.recipe.ModRecipesType;
import net.minecraft.advancements.Advancement;
import net.minecraft.data.IFinishedRecipe;
import net.minecraft.fluid.Fluid;
import net.minecraft.item.Item;
import net.minecraft.item.crafting.IRecipeSerializer;
import net.minecraft.item.crafting.Ingredient;
import net.minecraft.potion.Effect;
import net.minecraft.potion.EffectInstance;
import net.minecraft.tags.ITag;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.registries.ForgeRegistries;

import javax.annotation.Nullable;
import java.util.List;
import java.util.function.Consumer;

public class EffectAssemblyRecipeBuilder extends AssemblyRecipeBuilder<EffectAssemblyRecipeBuilder>{
    private final EffectInstance effect;

    public EffectAssemblyRecipeBuilder(Effect effect, int lvl, int tick) {
        super(ItemList.EFFECT_STORAGE_ITEM.get(), 1);
        this.effect = new EffectInstance(effect, tick, lvl, false, false);
    }

    public static EffectAssemblyRecipeBuilder recipe(Effect effect, int lvl, int tick) {
        return new EffectAssemblyRecipeBuilder(effect, lvl, tick);
    }

    @Override
    public void build(Consumer<IFinishedRecipe> consumer, ResourceLocation id) {
        id = new ResourceLocation(id.getNamespace(), "effect/" + id.getPath());
        super.build(consumer, id);
    }

    @Override
    public IFinishedRecipe getResult(ResourceLocation id) {
        return new Result(id, effect, result, count, group == null ? "" : group, backing, ingredients, fluid, fluidTag, fluidCount, energy, tick, advancementBuilder);
    }

    @Override
    public void validate(ResourceLocation id) {
        super.validate(id);
        if (effect == null) {
            throw new IllegalStateException("未定义效果(effect) " + id);
        }
    }

    public static class Result extends AssemblyRecipeBuilder.Result {
        private final EffectInstance effect;

        public Result(ResourceLocation id, EffectInstance effect, Item result, int count, String group, Ingredient backing, List<Ingredient> ingredients, @Nullable Fluid fluid, @Nullable ITag<Fluid> fluidTag, int fluidCount, int energy, int tick, Advancement.Builder advancementBuilder) {
            super(id, result, count, group, backing, ingredients, fluid, fluidTag, fluidCount, energy, tick, advancementBuilder);
            this.effect = effect;
        }

        @Override
        public void serialize(JsonObject json) {
            JsonObject effectJson = new JsonObject();
            effectJson.addProperty("effect", ForgeRegistries.POTIONS.getKey(effect.getPotion()).toString());
            effectJson.addProperty("lvl", effect.getAmplifier());
            effectJson.addProperty("tick", effect.getDuration());
            json.add("effect_result", effectJson);
            super.serialize(json);
        }

        @Override
        public IRecipeSerializer<?> getSerializer() {
            return ModRecipesType.EFFECT_ASSEMBLY_SERIALIZER.get();
        }
    }
}
